home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
5685
/
5685.xpi
/
chrome
/
vodpod.jar
/
content
/
bookmarklet.js
next >
Wrap
Text File
|
2008-01-08
|
10KB
|
272 lines
var vodpod = {
last_window : null,
root_window : null,
debug : false,
share_video: function(document, target, root_window, target_server, debug_val) {
try {
this.root_window = root_window;
this.debug = debug_val;
var embeds = vodpod.bookmarklet_scrape(document);
if (this.debug) {
alert("Found these embeds: " + embeds);
}
var title = vodpod.scrape_title(document);
var desc = vodpod.scrape_description(document);
var tags = vodpod.scrape_tags(document);
var my_form = document.createElement("form");
if (!target_server) {
target_server = "http://vodpod.com";
}
var target_path = "/widget/save_video_popup";
if (target == 'vodpod') {
target_path = "/widget/newptv";
}
my_form.action = target_server + target_path + "?target=" + escape(target) + '&bm=fe';
my_form.method = "POST";
my_form.target = "vodpod_share";
vodpod.create_hidden_field(document, my_form, 'p', document.location.href);
vodpod.create_hidden_field(document, my_form, 'v', 4);
for( var i = 0; i < embeds.length; i++ ){
vodpod.create_hidden_field(document, my_form, 'embed[]', embeds[i]);
}
if (embeds.length > 0){
vodpod.create_hidden_field(document, my_form, 'em', embeds[0]);
}
vodpod.create_hidden_field(document, my_form, 't', document.title);
vodpod.create_hidden_field(document, my_form, 'd', desc);
vodpod.create_hidden_field(document, my_form, 'tags', tags);
document.body.appendChild(my_form);
if (this.last_window) {
this.last_window.close();
}
var win_height = 550;
if (target == 'vodpod') {
win_height = 623;
}
this.last_window = root_window.open('','vodpod_share','toolbar=0,status=0,resizable=1,width=660,height=' + win_height);
my_form.submit();
} catch(e) {
alert("There was a problem opening the vodpod window [" + e + "]");
}
},
create_hidden_field : function(document, the_form, name, value) {
var hiddenf = document.createElement("input");
hiddenf.type = 'hidden';
hiddenf.name = name;
hiddenf.value = value;
the_form.appendChild(hiddenf);
},
scrape_title : function(document) {
return document.title;
},
scrape_description : function(document) {
var elt = vodpod.getElementsByClassName(document, 'videoDescDiv expand-content');
if (elt[0]) {
return elt[0].innerHTML;
} else {
if ( (elt = document.getElementById('description')) ){ // google
return elt.innerHTML;
} else if ( (elt = vodpod.getElementsByClassName(document, 'description'))){ // dailymotion
if (elt[0]) {
return elt[0].innerHTML;
}
}
}
return '';
},
scrape_tags : function(document) {
var tags = '';
var elt = vodpod.getElementsByClassName(document, 'tagValues'); //youtube
if (elt.length > 0){
elt = elt[0];
} else {
elt = null;
}
if (!elt) {
elt = document.getElementById('common_tags_short'); //google
}
if (!elt) {
elt = vodpod.getElementsByClassName(document, 'dm_widget_taglist');
if (elt.length > 0){
elt = elt[0];
}
}
if (elt && elt.childNodes) {
for( var i = 0; i < elt.childNodes.length; i++ ){
var child = elt.childNodes[i];
if (child.tagName && child.tagName.toLowerCase() == 'a' ){
tags = tags + child.innerHTML + ' ';
}
}
}
//alert("Returning tags: "+ tags);
return tags;
},
bookmarklet_scrape: function(document) {
b=document.body.innerHTML;
ms = b.match(new RegExp('&'+'lt;embed.*?&'+'gt;', 'im'));
if(!ms){
ms=b.match(/\<embed.*?\>/im);
}
if (!ms) {
ms = new Array();
}
// Here's a nice trick. Some people (I'm looking at your Eyespot!) have the input field with the embed code,
// but set the field value using JS. So here we look for any input fields that have an embed/object code
// as their value.
inp_fields = document.getElementsByTagName("input");
for( var i = 0; i < inp_fields.length; i++ ){
val = inp_fields[i].value;
if (val.match(/\<embed.*/im)) {
ms.push(val);
}
}
// Now use the dom to find embeds
dom_ms = document.getElementsByTagName("embed");
// Now build a list of possible embeds. Our algorithm is to take
// all embeds of approximately the right aspect ratio (4x3), then
// rank them by size
for (var i = 0; i < dom_ms.length; i++ ){
ms.push(dom_ms[i]);
}
if (ms.length == 0) {
// Look for object tags if no embeds
obj_tags = document.getElementsByTagName("object");
for( var j = 0; j < obj_tags.length; j++ ){
if (obj_tags[j].type == "application/x-shockwave-flash") {
var em_tag = '<embed src="' + obj_tags[j].data + '" type="application/x-shockwave-flash"' +
' height="' + obj_tags[j].height + '" width="' + obj_tags[j].width + '"';
var params = obj_tags[j].getElementsByTagName("param");
var flashvars = '';
for (var p = 0; p < params.length; p++) {
if (params[p].name == 'flashvars'){
flashvars = params[p].value;
break;
}
}
em_tag = em_tag + ' flashvars="' + flashvars + '">';
//alert("Found object tag, converted to embed: " + em_tag);
ms.push(em_tag);
}
}
}
//alert("MS: " + ms);
var candidates = new Array();
for( var i = 0; i < ms.length; i++ ){
var target = ms[i];
if (target.src) {
width = parseInt(target.width)
height = parseInt(target.height)
is_absolute = (target.src.match(/src=['"]\s*http/) != null);
if (width > 0 && height > 0){
aspect = width / height
if (aspect >= 1.0 && aspect <= 2.0) {
// Convert embed to html
ht = vodpod.element_to_html(target);
candidates.push([width,is_absolute,ht]);
}
}
} else {
// This is an embed found by src
try {
if (target.match(/</)){
target = this.unescapeHTML(document,target);
}
width = vodpod.grep_dimension(target, /width=['"](\d+)/);
height = vodpod.grep_dimension(target, /height=['"](\d+)/);
is_absolute = (target.match(/src=['"]\s*http/) != null);
if (width > 0 && height > 0){
aspect = width / height;
if (aspect >= 1.0 && aspect <= 2.3) {
candidates.push([width, is_absolute, target]);
}
} else {
//alert("discarding target: " + target);
}
} catch (e) {}
}
}
// Now sort the candidates by widest
candidates.sort( function(a,b) {
if (a[1] && !b[1]) {
return -1;
}
if (b[1] && !a[1]) {
return 1;
}
return b[0] - a[0];
} );
//alert(candidates);
result = new Array();
for( var i = 0; i < candidates.length; i++ ){
result.push(candidates[i][2]);
}
if (result.length == 0) {
// Didn't find anything, let's try to scrape frames...
var frames = document.getElementsByTagName("frame");
for( var f = 0; f < frames.length; f++ ){
try {
result = result.concat(this.bookmarklet_scrape(frames[f].contentDocument));
} catch (e) {}
}
}
return result;
},
grep_dimension : function(target, pattern) {
grep = target.match(pattern)
if (grep) {
return parseInt(grep[1]);
} else {
return 0;
}
},
element_to_html : function(element) {
var res = "<" + element.tagName + " ";
for( var i = 0; i < element.attributes.length; i++ ){
res = res + element.attributes[i].name + '="' + element.attributes[i].value + '" ';
}
res = res + "/>";
return res;
},
unescapeHTML : function(document, s) {
var div = document.createElement('div');
div.innerHTML = s;
var res = this.getTreeValues(div);
return res;
},
getTreeValues : function(elt) {
var res = '';
for( var i = 0; i < elt.childNodes.length; i++ ){
res = res + elt.childNodes[i].nodeValue;
res = res + this.getTreeValues(elt.childNodes[i]);
}
return res;
},
getElementsByClassName:function(root, className) {
var children = root.getElementsByTagName('*');
var elements = new Array();
for( var i = 0; i < children.length; i++ ){
var child = children[i];
if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))){
elements.push(child);
}
}
return elements;
}
};